//
//  PVDPhotoViewController.swift
//  ezyform
//
//  Created by MoennigQ on 17/5/18.
//  Copyright © 2018 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import MBProgressHUD
import Alamofire
import SDWebImage
class PVDPhotoViewController: BaseViewController {
    let customView = PVDPhotoView()
    var imageModel: BrowseImageData!
    var loadFlag: Bool = false//是否已经加载过，每个图片只加载一次
    var refreshTitleClosure: (()->Void)?
    init(_ imageModel: BrowseImageData) {
        super.init(nibName: nil, bundle: nil)
        self.imageModel = imageModel;
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view = self.customView
        self.customView.loadFieldView.retryBtn.addTarget(self, action: #selector(retryBtnClick), for: UIControl.Event.touchUpInside)
        //
    }
    func loadData(){
        if self.imageModel.image.uppercased().contains("HTTP") {//server image
            guard let url = URL(string: self.imageModel.image) else {
                return
            }
            //此处不能判断是否有网络，因为没有网络的时候要用图片缓存
            ModelViewUtil.shared.showLoadingView()
            self.customView.iconView.sd_setImage(with: url, placeholderImage: nil, options: [SDWebImageOptions.scaleDownLargeImages]) { [weak self](image, error, cashtype, url) in
                ModelViewUtil.shared.hideLoadingView()
                guard let strongSelf = self else {
                    return
                }
                
                if let _ = image {
                    strongSelf.customView.resizeCodeImageView(false)
                }else {
                    strongSelf.customView.resizeCodeImageView(true)
                }
            }
            
        }else{//Local image
            if let image = UIImage(contentsOfFile: imageModel.image) {
                self.customView.iconView.image = image
                self.customView.resizeCodeImageView(false)
            }else{
                self.customView.resizeCodeImageView(true)
            }
        }
        //panel，inverter的code是否符合验证规则，不符合要显示为红色
        var validFlag: Bool = true
        if self.imageModel.codeType == .Panel {
            validFlag = Tool().isValidPanelNumber(str: self.imageModel.code)
        }else if self.imageModel.codeType == CodeType.Inverter {
            validFlag = Tool().isValidInverterNumber(str: self.imageModel.code)
        }
        self.customView.showMsgLabel.textColor = validFlag ? .white : .red
        self.customView.showMsgLabel.text = imageModel.code
    }
    @objc func retryBtnClick(){
        self.loadData() 
    }
    //该方法在通知控制器已经布局 view 的子控件时调用，该方法调用时，AutoLayout 已经完成
    override func viewDidLayoutSubviews() {
        if !self.loadFlag {
            self.loadFlag = true
            self.loadData()
        }
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: false)
        self.refreshTitleClosure?()
    }
}

